package com.mx.mxweb.controller;

import com.github.pagehelper.PageInfo;
import com.mx.mxweb.dao.ResourcesMapper;
import com.mx.mxweb.model.Resources;
import com.mx.mxweb.service.ResService;
import com.mx.mxweb.vo.LayuiPageVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.List;

/**
 * 资源控制
 *
 * @author 李和平
 * @create 2018-08-16 15:31
 **/
@Controller
@RequestMapping(value = "/res")
public class ResController {

    @Autowired
    private ResService resService;

    @Autowired
    private ResourcesMapper resourcesMapper;

    @GetMapping(value = "")
    public String index(HttpServletRequest request,
                        HttpServletResponse response,
                        Model model){

        return "search";
    }

    @GetMapping(value = "pagelist")
    @ResponseBody
    public LayuiPageVo list(HttpServletRequest request,
                            HttpServletResponse response,
                            Resources resources,
                            String key,
                            @RequestParam(value = "currentPage", defaultValue = "1") int currentPage,
                            @RequestParam(value = "pageSize", defaultValue = "10") int pageSize){
        try {
            key = URLDecoder.decode(key,"UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        LayuiPageVo lpv = new LayuiPageVo();
        if (StringUtils.isBlank(key)){
            lpv.setCode(0);
            lpv.setMsg("获取信息失败");
            lpv.setTotal(null);
            lpv.setData("不要带空直接访问老子的接口");
            return lpv;
        }

        if(key.equals("李和平牛逼")||key.equals("张应祥牛逼")){
            key = "";
        }


        PageInfo<Resources> pageInfo = resService.seleteByKey(key,resources,currentPage,pageSize);



        lpv.setCode(1);
        lpv.setMsg("获取信息成功");
        lpv.setTotal(pageInfo.getTotal());
        lpv.setData(pageInfo.getList());
        return lpv;

    }

}
